﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NHibernate;
using hbse.Entities;
using hbse.Service;
using hbse.DTO;

namespace hbse
{
    public partial class Usfloderproduct : UserControl
    {
        private TabItem newTab;
        private DevComponents.DotNetBar.TabControl tabControl2;
        private ISession session;
        private UntilService untilService = new UntilService();
        private SampleFolderMService sampleFolderMService = new SampleFolderMService();


        private BarTender.ApplicationClass btApp;
        private BarTender.Format btformat;

        private string productCode;
        private Product mproduct;
        private Users users;

        public Usfloderproduct(DevComponents.DotNetBar.TabControl _tabControl2, TabItem _newTab, ISession _session,Users _user)
        {
            InitializeComponent();
            newTab = _newTab;
            tabControl2 = _tabControl2;
            session = _session;
            users = _user;

            try
            {
                  btApp = new BarTender.ApplicationClass();
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        private void cmdsearch_Click(object sender, EventArgs e)
        {
            List<hbse.DTO.ProductDTO> productDTOs = untilService.getProductsByCodeT10(txtsearch.Text.Trim(), session);

            if (productDTOs.Count > 0)
            {
                dgvProduct.DataSource = productDTOs;
                FormatdgvProcuct();
            }
            else
            {
                dgvProduct.DataSource = null;
                FormatdgvProcuct();

            }

        }

        private void FormatdgvProcuct()
        {
            if (dgvProduct.RowCount > 0)
            {
                dgvProduct.Columns["ProductCode"].HeaderText = "ProductCode";
                dgvProduct.Columns["CustomerCode"].HeaderText = "CustomerCode";

                dgvProduct.Columns["Id"].Visible = false;

            }
        }

        private void dgvProduct_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }

            productCode = dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString();

            List<hbse.DTO.ProductSamMDTO> productSamMDTOs = untilService.getProductsSamMDTOByCodepType(dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString(), session);
            if (productSamMDTOs.Count > 0)
            {
                dgvProduct2.DataSource = productSamMDTOs;
                FormatdgvProcuct2();
            }
            else
            {
                dgvProduct2.DataSource = null;
                FormatdgvProcuct2();

            }

            mproduct = sampleFolderMService.getByCode(dgvProduct.Rows[e.RowIndex].Cells["ProductCode"].Value.ToString(), session);
            txtdesc.Text = mproduct.ProductDescription;
            txtadditional.Text = mproduct.AdditionalProcess;


        }
        private void FormatdgvProcuct2()
        {
            if (dgvProduct2.RowCount > 0)
            {
                dgvProduct2.Columns["ProductCode"].HeaderText = "ProductCode";
                dgvProduct2.Columns["AC"].HeaderText = "AC";
                dgvProduct2.Columns["AR"].HeaderText = "AR";
                dgvProduct2.Columns["GAB"].HeaderText = "GAB";
                dgvProduct2.Columns["CustomerCode"].HeaderText = "CustomerCode";

                dgvProduct2.Columns["Id"].Visible = false;

            }
        }

        private void cmdprint_Click(object sender, EventArgs e)
        {
            if (productCode == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาเลือกข้อมูลก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                dgvProduct.Focus();
                return;
            }
            if (txtNumPrint.Text.Trim()== "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("กรุณาระบุจำนวนก่อน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtNumPrint.Focus();
                return;
            }

            List<ProductPrint> _productPrints = new List<ProductPrint>();
            ProductPrint productPrint;
            int j = 1;
            int count = Convert.ToInt32(txtNumPrint.Text.Trim());

            for (int i = 0; i < count; i++)
            {
                productPrint = new ProductPrint();
                productPrint.Users = users;
                productPrint.Product = mproduct;
                productPrint.NumPrint = 1;
                productPrint.PrintDate = DateTime.Now;
                _productPrints.Add(productPrint);

                try
                {
                    //1. ตรวจสอบ Item or Set
                    if (mproduct.ProductType == "0" || mproduct.ProductType == "1")
                    {
                        // ไม่มีลูก
                        string path = Application.StartupPath + "\\SampleFolderProductItem.btw";

                        btApp.Visible = false;
                        btformat = btApp.Formats.Open(path, true, "");

                        // barcode 
                        btformat.SetNamedSubStringValue("productcode", mproduct.ProductCode);
                        btformat.SetNamedSubStringValue("customercode", mproduct.Customer.CustomerCode);
                        btformat.SetNamedSubStringValue("productdesc", mproduct.ProductDescription);
                        btformat.SetNamedSubStringValue("productac", mproduct.ProductAc);
                        btformat.SetNamedSubStringValue("productar", mproduct.ProductAr);
                        btformat.SetNamedSubStringValue("productgab", mproduct.ProductGab);
                        btformat.SetNamedSubStringValue("productadd", mproduct.AdditionalProcess);  
                        
                        btformat.PrintOut(false, false);


                    }
                    else  
                    {
                        // มีลูก
                        string path = Application.StartupPath + "\\SampleFolderProductSet.btw";

                        btApp.Visible = false;
                        btformat = btApp.Formats.Open(path, true, "");

                        // barcode 
                        btformat.SetNamedSubStringValue("productcode1", mproduct.ProductCode);
                        btformat.SetNamedSubStringValue("productcode2", mproduct.ProductCode);
                        btformat.SetNamedSubStringValue("productcode3", mproduct.ProductCode);

                        btformat.SetNamedSubStringValue("productac1", mproduct.ProductAc);
                        btformat.SetNamedSubStringValue("productac2", mproduct.ProductAc);
                        btformat.SetNamedSubStringValue("productac3", mproduct.ProductAc);

                        btformat.SetNamedSubStringValue("productar1", mproduct.ProductAr);
                        btformat.SetNamedSubStringValue("productar2", mproduct.ProductAr);
                        btformat.SetNamedSubStringValue("productar3", mproduct.ProductAr);

                        btformat.SetNamedSubStringValue("productgab1", mproduct.ProductGab);
                        btformat.SetNamedSubStringValue("productgab2", mproduct.ProductGab);
                        btformat.SetNamedSubStringValue("productgab3", mproduct.ProductGab);

                        btformat.SetNamedSubStringValue("customercode1", mproduct.Customer.CustomerCode);
                        btformat.SetNamedSubStringValue("customercode2", mproduct.Customer.CustomerCode);
                        btformat.SetNamedSubStringValue("customercode3", mproduct.Customer.CustomerCode);

                        btformat.SetNamedSubStringValue("productdesc", mproduct.ProductDescription);
                        btformat.SetNamedSubStringValue("productadd", mproduct.AdditionalProcess);

                        btformat.PrintOut(false, false);

                    }

                    //print barcode

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    btformat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
                }   


            }
            mproduct.ProductPrints.Clear();
            mproduct.ProductPrints = _productPrints;

            //var productprints = session.CreateQuery("from ProductPrint ps where ps.Product.Id=1").Future<ProductPrint>();


            int result = sampleFolderMService.editProduct(mproduct, session);
            if (result > -1)
            {
                Console.WriteLine("Update Complete");
                MessageBox.Show("บันทึกข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

               
            }
            else
            {
                Console.WriteLine("Update Not Complete");
                MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้ !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

    }
}
